/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             DbLogic.cs
*        创建系统时间:       2012/11/2 19:07:01
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Utilities;

    public class DbLogic
    {
        public static int BatchDelete(IDbHelper dbHelper, string tableName, string[] ids)
        {
            int num = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                num += Delete(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
            }
            return num;
        }

        public static int BatchDelete(IDbHelper dbHelper, string tableName, string[] names, object[] values)
        {
            string sqlQuery = " DELETE  FROM " + tableName;
            string str2 = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
            if (str2.Length > 0)
            {
                sqlQuery = sqlQuery + " WHERE " + str2;
            }
            return dbHelper.ExecuteNonQuery(sqlQuery, dbHelper.MakeParameters(names, values));
        }

        public static int BatchDelete(IDbHelper dbHelper, string tableName, string name, object[] values)
        {
            int num = 0;
            for (int i = 0; i < values.Length; i++)
            {
                num += Delete(dbHelper, tableName, name, values[i]);
            }
            return num;
        }

        public static int Delete(IDbHelper dbHelper, string tableName)
        {
            return BatchDelete(dbHelper, tableName, new string[0], new object[0]);
        }

        public static int Delete(IDbHelper dbHelper, string tableName, string id)
        {
            return Delete(dbHelper, tableName, BaseBusinessLogic.FieldID, id);
        }

        public static int Delete(IDbHelper dbHelper, string tableName, string name, object value)
        {
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return BatchDelete(dbHelper, tableName, names, values);
        }

        public static int Delete(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2)
        {
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            names[1] = name2;
            values[0] = value1;
            values[1] = value2;
            return BatchDelete(dbHelper, tableName, names, values);
        }

        public static int ExecuteNonQuery(IDbHelper dbHelper, string[] sqlQuerys)
        {
            int num = 0;
            string sqlQuery = string.Empty;
            for (int i = 0; i < sqlQuerys.Length; i++)
            {
                sqlQuery = sqlQuerys[i];
                num += ExecuteNonQuery(dbHelper, sqlQuery);
            }
            return num;
        }

        public static int ExecuteNonQuery(IDbHelper dbHelper, string sqlQuery)
        {
            return dbHelper.ExecuteNonQuery(sqlQuery);
        }

        public static string ExecuteScalar(IDbHelper dbHelper, string sqlQuery)
        {
            string str = string.Empty;
            object obj2 = dbHelper.ExecuteScalar(sqlQuery);
            if (obj2 != null)
            {
                str = obj2.ToString();
            }
            return str;
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string[] ids)
        {
            bool flag = false;
            for (int i = 0; i < ids.Length; i++)
            {
                flag = Exists(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
                if (flag)
                {
                    return flag;
                }
            }
            return flag;
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string id)
        {
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = BaseBusinessLogic.FieldID;
            values[0] = id;
            return Exists(dbHelper, tableName, names, values);
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string name, object value)
        {
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return Exists(dbHelper, tableName, names, values);
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string name, string[] values)
        {
            bool flag = false;
            for (int i = 0; i < values.Length; i++)
            {
                flag = Exists(dbHelper, tableName, name, values[i]);
                if (flag)
                {
                    return flag;
                }
            }
            return flag;
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string[] names, object[] values)
        {
            bool flag = false;
            string sqlQuery = " SELECT COUNT(*)  FROM " + tableName + " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
            object obj2 = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
            if (obj2 != null)
            {
                flag = int.Parse(obj2.ToString()) > 0;
            }
            return flag;
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string name, object value, string id)
        {
            bool flag = false;
            string sqlQuery = " SELECT COUNT(*) FROM " + tableName;
            if (name == null)
            {
                sqlQuery = sqlQuery + " WHERE (" + name + " IS NULL ) ";
            }
            else
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " WHERE (" + name + " = " + dbHelper.GetParameter(name) + ") ";
            }
            if (id.Length > 0)
            {
                string str3 = sqlQuery;
                sqlQuery = str3 + BaseBusinessLogic.SQLLogicConditional + "( " + BaseBusinessLogic.FieldID + " <> " + dbHelper.GetParameter(BaseBusinessLogic.FieldID) + " ) ";
            }
            string[] targetFileds = new string[2];
            object[] targetValues = new object[2];
            targetFileds[0] = name;
            targetValues[0] = value;
            if (id.Length > 0)
            {
                targetFileds[1] = BaseBusinessLogic.FieldID;
                targetValues[1] = id;
            }
            object obj2 = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(targetFileds, targetValues));
            if (obj2 != null)
            {
                flag = int.Parse(obj2.ToString()) > 0;
            }
            return flag;
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2)
        {
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            names[1] = name2;
            values[0] = value1;
            values[1] = value2;
            return Exists(dbHelper, tableName, names, values);
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string[] names, object[] values, string name, string id)
        {
            bool flag = false;
            string str2 = " SELECT COUNT(*)  FROM " + tableName + " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
            string sqlQuery = str2 + BaseBusinessLogic.SQLLogicConditional + "( " + BaseBusinessLogic.FieldID + " <> '" + id + "' ) ";
            object obj2 = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
            if (obj2 != null)
            {
                flag = int.Parse(obj2.ToString()) > 0;
            }
            return flag;
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2, string id)
        {
            return Exists(dbHelper, tableName, name1, value1, name2, value2, BaseBusinessLogic.FieldID, id);
        }

        public static bool Exists(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2, string name, string id)
        {
            bool flag = false;
            string sqlQuery = " SELECT COUNT(*) FROM " + tableName;
            if (value1 == null)
            {
                sqlQuery = sqlQuery + " WHERE (" + name1 + " IS NULL) ";
            }
            else
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " WHERE (" + name1 + " = " + dbHelper.GetParameter(name1) + ") ";
            }
            if (value2 == null)
            {
                string str3 = sqlQuery;
                sqlQuery = str3 + BaseBusinessLogic.SQLLogicConditional + "(" + name2 + " IS NULL) ";
            }
            else
            {
                string str4 = sqlQuery;
                sqlQuery = str4 + BaseBusinessLogic.SQLLogicConditional + "(" + name2 + " = " + dbHelper.GetParameter(name2) + ") ";
            }
            if (id.Length > 0)
            {
                string str5 = sqlQuery;
                sqlQuery = str5 + BaseBusinessLogic.SQLLogicConditional + "( " + name + " <> " + dbHelper.GetParameter(name) + ") ";
            }
            string[] targetFileds = new string[3];
            object[] targetValues = new object[3];
            targetFileds[0] = name1;
            targetValues[0] = value1;
            targetFileds[1] = name2;
            targetValues[1] = value2;
            if (id.Length > 0)
            {
                targetFileds[2] = name;
                targetValues[2] = id;
            }
            object obj2 = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(targetFileds, targetValues));
            if (obj2 != null)
            {
                flag = int.Parse(obj2.ToString()) > 0;
            }
            return flag;
        }

        public static string FindID(IDbHelper dbHelper, string tableName, string[] names, object[] values)
        {
            return GetProperty(dbHelper, tableName, names, values, BaseBusinessLogic.FieldID);
        }

        public static string FindID(IDbHelper dbHelper, string tableName, string name, object value)
        {
            return GetProperty(dbHelper, tableName, name, value, BaseBusinessLogic.FieldID);
        }

        public static string FindID(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2)
        {
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            names[1] = name2;
            values[0] = value1;
            values[1] = value2;
            return GetProperty(dbHelper, tableName, names, values, BaseBusinessLogic.FieldID);
        }

        public static DataTable GetChildrens(IDbHelper dbHelper, string tableName, string fieldID, string id, string fieldParentID, string order)
        {
            return GetChildrens(dbHelper, tableName, fieldID, id, fieldParentID, order, false);
        }

        public static DataTable GetChildrens(IDbHelper dbHelper, string tableName, string fieldID, string[] ids, string fieldParentID, string order, bool idOnly)
        {
            string sqlQuery = string.Empty;
            if (idOnly)
            {
                sqlQuery = "   SELECT " + fieldID;
            }
            else
            {
                sqlQuery = "   SELECT * ";
            }
            string str2 = sqlQuery;
            sqlQuery = str2 + "          FROM " + tableName + "    START WITH " + fieldID + " IN (" + BaseBusinessLogic.ObjectsToList(ids) + ")  CONNECT BY PRIOR " + fieldID + " = " + fieldParentID;
            if (!string.IsNullOrEmpty(order))
            {
                sqlQuery = sqlQuery + " ORDER BY " + order;
            }
            DataTable dataTable = new DataTable(tableName);
            dbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public static DataTable GetChildrens(IDbHelper dbHelper, string tableName, string fieldID, string id, string fieldParentID, string order, bool idOnly)
        {
            string sqlQuery = string.Empty;
            if (idOnly)
            {
                sqlQuery = "   SELECT " + fieldID;
            }
            else
            {
                sqlQuery = "   SELECT * ";
            }
            string str2 = sqlQuery;
            sqlQuery = str2 + "          FROM " + tableName + "    START WITH " + fieldID + " = " + dbHelper.GetParameter(fieldID) + "  CONNECT BY PRIOR " + fieldID + " = " + fieldParentID;
            if (!string.IsNullOrEmpty(order))
            {
                sqlQuery = sqlQuery + " ORDER BY " + order;
            }
            string[] targetFileds = new string[] { fieldID };
            object[] targetValues = new object[] { id };
            DataTable dataTable = new DataTable(tableName);
            dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public static DataTable GetChildrensByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order)
        {
            return GetChildrensByCode(dbHelper, tableName, fieldCode, code, order, false);
        }

        private static DataTable GetChildrensByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order, bool idOnly)
        {
            string sqlQuery = string.Empty;
            if (idOnly)
            {
                sqlQuery = "   SELECT " + BaseBusinessLogic.FieldID;
            }
            else
            {
                sqlQuery = "   SELECT * ";
            }
            sqlQuery = sqlQuery + "     FROM " + tableName;
            switch (BaseConfiguration.Instance.DataBaseType)
            {
                case DataBaseType.Oracle:
                    {
                        string str3 = sqlQuery;
                        sqlQuery = str3 + "    WHERE (SUBSTR(" + fieldCode + ", 1, LENGTH(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + " ) ";
                        break;
                    }
                case DataBaseType.Sqlserver:
                case DataBaseType.Access:
                    {
                        string str2 = sqlQuery;
                        sqlQuery = str2 + "    WHERE (LEFT(" + fieldCode + ", LEN(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + " ) ";
                        break;
                    }
            }
            if (!string.IsNullOrEmpty(order))
            {
                sqlQuery = sqlQuery + " ORDER BY " + order;
            }
            string[] targetFileds = new string[] { fieldCode, fieldCode };
            object[] targetValues = new object[] { code, code };
            DataTable dataTable = new DataTable(tableName);
            dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public static string[] GetChildrensID(IDbHelper dbHelper, string tableName, string fieldID, string id, string fieldParentID, string order)
        {
            return BaseBusinessLogic.FieldToArray(GetChildrens(dbHelper, tableName, fieldID, id, fieldParentID, order, true), BaseBusinessLogic.FieldID);
        }

        public static string[] GetChildrensIDByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order)
        {
            return BaseBusinessLogic.FieldToArray(GetChildrensByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName)
        {
            string order = string.Empty;
            int topLimit = 0;
            string[] names = new string[0];
            object[] values = new object[0];
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string order)
        {
            int topLimit = 0;
            string[] names = new string[0];
            object[] values = new object[0];
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, object[] ids)
        {
            return GetDT(dbHelper, tableName, BaseBusinessLogic.FieldID, ids);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string order, int topLimit)
        {
            string[] names = new string[0];
            object[] values = new object[0];
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string[] names, object[] values)
        {
            string order = string.Empty;
            int topLimit = 0;
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string name, object[] values)
        {
            return GetDT(dbHelper, tableName, name, values, string.Empty);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string name, object value)
        {
            string order = string.Empty;
            int topLimit = 0;
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, object[] ids, string order)
        {
            return GetDT(dbHelper, tableName, BaseBusinessLogic.FieldID, ids, order);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string name, object value, string order)
        {
            int topLimit = 0;
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string name, object[] values, string order)
        {
            DataTable dataTable = new DataTable(tableName);
            string sqlQuery = " SELECT *    FROM " + tableName + "  WHERE " + name + " IN (" + BaseBusinessLogic.ObjectsToList(values) + ")";
            if (!string.IsNullOrEmpty(order))
            {
                sqlQuery = sqlQuery + " ORDER BY " + order;
            }
            dbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string[] names, object[] values, string order)
        {
            int topLimit = 0;
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string name, object value, string order, int topLimit)
        {
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string[] names, object[] values, string order, int topLimit)
        {
            string sqlQuery = " SELECT * FROM " + tableName;
            string str2 = string.Empty;
            if (topLimit != 0)
            {
                switch (BaseConfiguration.Instance.DataBaseType)
                {
                    case DataBaseType.Oracle:
                        str2 = " ROWNUM < = " + topLimit;
                        break;

                    case DataBaseType.Sqlserver:
                    case DataBaseType.Access:
                        sqlQuery = " SELECT TOP " + topLimit.ToString() + " * FROM " + tableName;
                        break;
                }
            }
            string str3 = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
            if (str3.Length > 0)
            {
                if (str2.Length > 0)
                {
                    str2 = str2 + BaseBusinessLogic.SQLLogicConditional + str3;
                }
                else
                {
                    str2 = str3;
                }
            }
            if (str2.Length > 0)
            {
                sqlQuery = sqlQuery + " WHERE " + str2;
            }
            if ((order != null) && (order.Length > 0))
            {
                sqlQuery = sqlQuery + " ORDER BY " + order;
            }
            if ((topLimit != 0) && (BaseConfiguration.Instance.DataBaseType == DataBaseType.MySql))
            {
                sqlQuery = sqlQuery + " LIMIT 0, " + topLimit;
            }
            DataTable dataTable = new DataTable(tableName);
            dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
            return dataTable;
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2)
        {
            int topLimit = 0;
            string order = string.Empty;
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            values[0] = value1;
            names[1] = name2;
            values[1] = value2;
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2, string order)
        {
            int topLimit = 0;
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            values[0] = value1;
            names[1] = name2;
            values[1] = value2;
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetDT(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2, string order, int topLimit)
        {
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            values[0] = value1;
            names[1] = name2;
            values[1] = value2;
            return GetDT(dbHelper, tableName, names, values, order, topLimit);
        }

        public static DataTable GetFromProcedure(IDbHelper dbHelper, string procedureName, string tableName)
        {
            return dbHelper.ExecuteProcedureForDataTable(procedureName, tableName, null);
        }

        public static DataTable GetFromProcedure(IDbHelper dbHelper, string procedureName, string tableName, string id)
        {
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            targetFileds[0] = BaseBusinessLogic.FieldID;
            targetValues[0] = id;
            return dbHelper.ExecuteProcedureForDataTable(procedureName, tableName, dbHelper.MakeParameters(targetFileds, targetValues));
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName)
        {
            string targetField = string.Empty;
            int topLimit = 0;
            string[] names = new string[0];
            object[] values = new object[0];
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string targetField)
        {
            int topLimit = 0;
            string[] names = new string[0];
            object[] values = new object[0];
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string name, object value)
        {
            string targetField = string.Empty;
            int topLimit = 0;
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string[] names, object[] values)
        {
            string targetField = string.Empty;
            int topLimit = 0;
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string name, object[] values)
        {
            return GetIDs(dbHelper, tableName, name, values, BaseBusinessLogic.FieldID);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string targetField, int topLimit)
        {
            string[] names = new string[0];
            object[] values = new object[0];
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string name, object[] values, string targetField)
        {
            DataTable dataTable = new DataTable(tableName);
            string sqlQuery = " SELECT " + targetField + "   FROM " + tableName + "  WHERE " + name + " IN (" + BaseBusinessLogic.ObjectsToList(values) + ")";
            dbHelper.Fill(dataTable, sqlQuery);
            return BaseBusinessLogic.FieldToArray(dataTable, targetField);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string[] names, object[] values, string targetField)
        {
            int topLimit = 0;
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string name, object value, string targetField)
        {
            int topLimit = 0;
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string name, object value, string targetField, int topLimit)
        {
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2)
        {
            int topLimit = 0;
            string targetField = string.Empty;
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            values[0] = value1;
            names[1] = name2;
            values[1] = value2;
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string[] names, object[] values, string targetField, int topLimit)
        {
            if (string.IsNullOrEmpty(targetField))
            {
                targetField = BaseBusinessLogic.FieldID;
            }
            string sqlQuery = " SELECT " + targetField + " FROM " + tableName;
            string str2 = string.Empty;
            if (topLimit != 0)
            {
                switch (BaseConfiguration.Instance.DataBaseType)
                {
                    case DataBaseType.Oracle:
                        str2 = " ROWNUM < = " + topLimit;
                        break;

                    case DataBaseType.Sqlserver:
                    case DataBaseType.Access:
                        sqlQuery = " SELECT TOP " + topLimit.ToString() + targetField + " FROM " + tableName;
                        break;
                }
            }
            string str3 = GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
            if (str3.Length > 0)
            {
                if (str2.Length > 0)
                {
                    str2 = str2 + BaseBusinessLogic.SQLLogicConditional + str3;
                }
                else
                {
                    str2 = str3;
                }
            }
            if (str2.Length > 0)
            {
                sqlQuery = sqlQuery + " WHERE " + str2;
            }
            if ((topLimit != 0) && (BaseConfiguration.Instance.DataBaseType == DataBaseType.MySql))
            {
                sqlQuery = sqlQuery + " LIMIT 0, " + topLimit;
            }
            DataTable dataTable = new DataTable(tableName);
            dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(names, values));
            return BaseBusinessLogic.FieldToArray(dataTable, targetField);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2, string targetField)
        {
            int topLimit = 0;
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            values[0] = value1;
            names[1] = name2;
            values[1] = value2;
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static string[] GetIDs(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2, string targetField, int topLimit)
        {
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            values[0] = value1;
            names[1] = name2;
            values[1] = value2;
            return GetIDs(dbHelper, tableName, names, values, targetField, topLimit);
        }

        public static DataTable GetParentChildrensByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order)
        {
            return GetParentChildrensByCode(dbHelper, tableName, fieldCode, code, order, false);
        }

        private static DataTable GetParentChildrensByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order, bool idOnly)
        {
            string sqlQuery = string.Empty;
            if (idOnly)
            {
                sqlQuery = "   SELECT " + BaseBusinessLogic.FieldID;
            }
            else
            {
                sqlQuery = "   SELECT * ";
            }
            sqlQuery = sqlQuery + "     FROM " + tableName;
            switch (BaseConfiguration.Instance.DataBaseType)
            {
                case DataBaseType.Oracle:
                    {
                        string str4 = sqlQuery;
                        string str5 = str4 + "    WHERE (SUBSTR(" + fieldCode + ", 1, LENGTH(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + ") ";
                        sqlQuery = str5 + "          OR (" + fieldCode + " = SUBSTR(" + dbHelper.GetParameter(fieldCode) + ", 1, LENGTH(" + fieldCode + "))) ";
                        break;
                    }
                case DataBaseType.Sqlserver:
                case DataBaseType.Access:
                    {
                        string str2 = sqlQuery;
                        string str3 = str2 + "    WHERE (LEFT(" + fieldCode + ", LEN(" + dbHelper.GetParameter(fieldCode) + ")) = " + dbHelper.GetParameter(fieldCode) + ") ";
                        sqlQuery = str3 + "          OR (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ") ";
                        break;
                    }
            }
            if (!string.IsNullOrEmpty(order))
            {
                sqlQuery = sqlQuery + " ORDER BY " + order;
            }
            string[] targetFileds = new string[] { fieldCode, fieldCode, fieldCode };
            object[] targetValues = new object[] { code, code, code };
            DataTable dataTable = new DataTable();
            dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public static string[] GetParentChildrensIDByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order)
        {
            return BaseBusinessLogic.FieldToArray(GetParentChildrensByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
        }

        public static string GetParentIDByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code)
        {
            string str = string.Empty;
            string sqlQuery = " SELECT MAX(ID) AS ID     FROM " + tableName + "  WHERE (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ")     AND " + fieldCode + " <>  '" + code + " ' ";
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            targetFileds[0] = fieldCode;
            targetValues[0] = code;
            object obj2 = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(targetFileds, targetValues));
            if (obj2 != null)
            {
                str = obj2.ToString();
            }
            return str;
        }

        public static DataTable GetParentsByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order)
        {
            return GetParentsByCode(dbHelper, tableName, fieldCode, code, order, false);
        }

        public static DataTable GetParentsByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order, bool idOnly)
        {
            string sqlQuery = string.Empty;
            if (idOnly)
            {
                sqlQuery = "   SELECT " + BaseBusinessLogic.FieldID;
            }
            else
            {
                sqlQuery = "   SELECT * ";
            }
            sqlQuery = sqlQuery + "     FROM " + tableName;
            switch (BaseConfiguration.Instance.DataBaseType)
            {
                case DataBaseType.Oracle:
                    {
                        string str3 = sqlQuery;
                        sqlQuery = str3 + "    WHERE (SUBSTR(" + dbHelper.GetParameter(fieldCode) + ", 1, LENGTH(" + fieldCode + ")) = " + fieldCode + ") ";
                        break;
                    }
                case DataBaseType.Sqlserver:
                case DataBaseType.Access:
                    {
                        string str2 = sqlQuery;
                        sqlQuery = str2 + "    WHERE (LEFT(" + dbHelper.GetParameter(fieldCode) + ", LEN(" + fieldCode + ")) = " + fieldCode + ") ";
                        break;
                    }
            }
            if (!string.IsNullOrEmpty(order))
            {
                sqlQuery = sqlQuery + " ORDER BY " + order;
            }
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            targetFileds[0] = fieldCode;
            targetValues[0] = code;
            DataTable dataTable = new DataTable(tableName);
            dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public static string[] GetParentsIDByCode(IDbHelper dbHelper, string tableName, string fieldCode, string code, string order)
        {
            return BaseBusinessLogic.FieldToArray(GetParentsByCode(dbHelper, tableName, fieldCode, code, order, true), BaseBusinessLogic.FieldID);
        }

        public static string GetProperty(IDbHelper dbHelper, string tableName, string[] names, object[] values, string targetField)
        {
            string str = string.Empty;
            string sqlQuery = " SELECT " + targetField + " FROM " + tableName + " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional);
            object obj2 = dbHelper.ExecuteScalar(sqlQuery, dbHelper.MakeParameters(names, values));
            if (obj2 != null)
            {
                str = obj2.ToString();
            }
            return str;
        }

        public static string GetProperty(IDbHelper dbHelper, string tableName, string name, object value, string targetField)
        {
            string[] names = new string[1];
            object[] values = new object[1];
            names[0] = name;
            values[0] = value;
            return GetProperty(dbHelper, tableName, names, values, targetField);
        }

        public static string GetProperty(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2, string targetField)
        {
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            names[1] = name2;
            values[0] = value1;
            values[1] = value2;
            return GetProperty(dbHelper, tableName, names, values, targetField);
        }

        public static string GetWhereString(IDbHelper dbHelper, string[] names, ref object[] values, string relation)
        {
            string str = string.Empty;
            string str2 = string.Empty;
            for (int i = 0; i < names.Length; i++)
            {
                if ((names[i] != null) && (names[i].Length > 0))
                {
                    if (values[i] == null)
                    {
                        str2 = " (" + names[i] + " IS NULL) ";
                    }
                    else
                    {
                        str2 = " (" + names[i] + " = " + dbHelper.GetParameter(names[i]) + ") ";
                        if ((values[i].ToString().IndexOf('[') >= 0) || (values[i].ToString().IndexOf(']') >= 0))
                        {
                            values[i] = values[i].ToString().Replace("[", "/[");
                            values[i] = values[i].ToString().Replace("]", "/]");
                            values[i] = SqlSafe(values[i].ToString());
                            str2 = string.Concat(new object[] { " (", names[i], " LIKE '", values[i], "' ESCAPE '/') " });
                            values[i] = null;
                        }
                    }
                    str = str + str2 + relation;
                }
            }
            if (str.Length > 0)
            {
                str = str.Substring(0, (str.Length - relation.Length) - 1);
            }
            return str;
        }

        public static bool IsModifed(DataRow dataRow, string oldModifyUserID, string oldModifyDate)
        {
            bool flag = false;
            if ((dataRow[BaseBusinessLogic.FieldModifyUserID] == DBNull.Value) || (dataRow[BaseBusinessLogic.FieldModifyDate] == DBNull.Value))
            {
                return false;
            }
            if (dataRow[BaseBusinessLogic.FieldModifyUserID].ToString().Equals(oldModifyUserID) && dataRow[BaseBusinessLogic.FieldModifyDate].ToString().Equals(oldModifyDate))
            {
                return flag;
            }
            return true;
        }

        private static bool IsModifed(DataTable dataTable, string oldModifyUserID, string oldModifyDate)
        {
            bool flag = false;
            foreach (DataRow row in dataTable.Rows)
            {
                flag = IsModifed(row, oldModifyUserID, oldModifyDate);
            }
            return flag;
        }

        public static bool IsModifed(IDbHelper dbHelper, string tableName, string id, string oldModifyUserID, string oldModifyDate)
        {
            return IsModifed(dbHelper, tableName, BaseBusinessLogic.FieldID, id, oldModifyUserID, oldModifyDate);
        }

        public static bool IsModifed(IDbHelper dbHelper, string tableName, string fieldName, string fieldValue, string oldModifyUserID, string oldModifyDate)
        {
            DataTable dataTable = new DataTable(tableName);
            string sqlQuery = " SELECT " + BaseBusinessLogic.FieldID + "," + BaseBusinessLogic.FieldCreateUserID + "," + BaseBusinessLogic.FieldCreateDate + "," + BaseBusinessLogic.FieldModifyUserID + "," + BaseBusinessLogic.FieldModifyDate + " FROM " + tableName + " WHERE " + fieldName + " = " + dbHelper.GetParameter(fieldName);
            dbHelper.Fill(dataTable, sqlQuery, dbHelper.MakeParameters(fieldName, fieldValue));
            return IsModifed(dataTable, oldModifyUserID, oldModifyDate);
        }

        public static int LockNoWait(IDbHelper dbHelper, string tableName, string[] ids)
        {
            int num = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                num += LockNoWait(dbHelper, tableName, BaseBusinessLogic.FieldID, ids[i]);
            }
            return num;
        }

        public static int LockNoWait(IDbHelper dbHelper, string tableName, string id)
        {
            return LockNoWait(dbHelper, tableName, BaseBusinessLogic.FieldID, id);
        }

        public static int LockNoWait(IDbHelper dbHelper, string tableName, string name, object value)
        {
            string sqlQuery = " SELECT " + BaseBusinessLogic.FieldID + " FROM " + tableName;
            if (name == null)
            {
                sqlQuery = sqlQuery + " WHERE (" + name + " IS NULL ) ";
            }
            else
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " WHERE (" + name + " = " + dbHelper.GetParameter(name) + ")";
            }
            sqlQuery = sqlQuery + " FOR UPDATE NOWAIT ";
            try
            {
                DataSet dataSet = new DataSet();
                dbHelper.Fill(dataSet, sqlQuery, "ForUpdateNoWait", dbHelper.MakeParameters(name, value));
                return dataSet.Tables["ForUpdateNoWait"].Rows.Count;
            }
            catch
            {
                return -1;
            }
        }

        public static int LockNoWait(IDbHelper dbHelper, string tableName, string[] names, object[] values)
        {
            string sqlQuery = (" SELECT " + BaseBusinessLogic.FieldID + " FROM " + tableName + " WHERE " + GetWhereString(dbHelper, names, ref values, BaseBusinessLogic.SQLLogicConditional)) + " FOR UPDATE NOWAIT ";
            try
            {
                DataSet dataSet = new DataSet();
                dbHelper.Fill(dataSet, sqlQuery, "ForUpdateNoWait", dbHelper.MakeParameters(names, values));
                return dataSet.Tables["ForUpdateNoWait"].Rows.Count;
            }
            catch
            {
                return -1;
            }
        }

        public static int LockNoWait(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2)
        {
            string[] names = new string[2];
            object[] values = new object[2];
            names[0] = name1;
            names[1] = name2;
            values[0] = value1;
            values[1] = value2;
            return LockNoWait(dbHelper, tableName, names, values);
        }

        public static int SetProperty(IDbHelper dbHelper, string tableName, string targetField, object targetValue)
        {
            SQLBuilder builder = new SQLBuilder(dbHelper);
            builder.BeginUpdate(tableName);
            builder.SetValue(targetField, targetValue);
            return builder.EndUpdate();
        }

        public static int SetProperty(IDbHelper dbHelper, string tableName, string[] names, object[] values, string targetField, object targetValue)
        {
            SQLBuilder builder = new SQLBuilder(dbHelper);
            builder.BeginUpdate(tableName);
            builder.SetValue(targetField, targetValue);
            builder.SetDBNow(BaseBusinessLogic.FieldModifyDate);
            builder.SetWhere(names, values);
            return builder.EndUpdate();
        }

        public static int SetProperty(IDbHelper dbHelper, string tableName, string name, object[] values, string targetField, object targetValue)
        {
            int num = 0;
            for (int i = 0; i < values.Length; i++)
            {
                num += SetProperty(dbHelper, tableName, name, values[i], targetField, targetValue);
            }
            return num;
        }

        public static int SetProperty(IDbHelper dbHelper, string tableName, string name, object value, string targetField, object targetValue)
        {
            SQLBuilder builder = new SQLBuilder(dbHelper);
            builder.BeginUpdate(tableName);
            builder.SetValue(targetField, targetValue);
            builder.SetWhere(name, value);
            return builder.EndUpdate();
        }

        public static int SetProperty(IDbHelper dbHelper, string tableName, string name, object value, string[] targetFields, object[] targetValues)
        {
            SQLBuilder builder = new SQLBuilder(dbHelper);
            builder.BeginUpdate(tableName);
            for (int i = 0; i < targetFields.Length; i++)
            {
                builder.SetValue(targetFields[i], targetValues[i]);
            }
            builder.SetDBNow(BaseBusinessLogic.FieldModifyDate);
            builder.SetWhere(name, value);
            return builder.EndUpdate();
        }

        public static int SetProperty(IDbHelper dbHelper, string tableName, string name1, object value1, string name2, object value2, string targetField, object targetValue)
        {
            SQLBuilder builder = new SQLBuilder(dbHelper);
            builder.BeginUpdate(tableName);
            builder.SetValue(targetField, targetValue);
            builder.SetWhere(name1, value1);
            builder.SetWhere(name2, value2);
            return builder.EndUpdate();
        }

        public static string SqlSafe(string value)
        {
            value = value.Replace("'", "''");
            return value;
        }

        public static int Truncate(IDbHelper dbHelper, string tableName)
        {
            string sqlQuery = " Truncate Table " + tableName;
            return dbHelper.ExecuteNonQuery(sqlQuery);
        }

        public static int UpdateRecord(IDbHelper dbHelper, string tableName, string name, string value, string targetField, object targetValue)
        {
            SQLBuilder builder = new SQLBuilder(dbHelper);
            builder.BeginUpdate(tableName);
            builder.SetValue(targetField, targetValue);
            builder.SetWhere(name, value);
            return builder.EndUpdate();
        }
    }
}